home *** CD-ROM | disk | FTP | other *** search
- package com.extensibility.exv;
-
- import com.extensibility.util.StringUtilities;
- import com.extensibility.xml.BaseDeclaration;
- import com.extensibility.xml.ExternalSubset;
- import com.extensibility.xml.ParserException;
- import com.extensibility.xml.Schema;
- import com.extensibility.xml.SchemaUtilities;
- import com.extensibility.xml.URI;
- import java.io.File;
- import java.io.Serializable;
- import java.io.StringWriter;
- import java.net.URL;
- import java.util.Enumeration;
- import java.util.ResourceBundle;
- import java.util.Vector;
-
- public class SchemaWrapper implements Serializable {
- public static final int FLAVOR_VIRGIN = 0;
- public static final int FLAVOR_STRING = 1;
- public static final int FLAVOR_URL = 2;
- public static final int FLAVOR_FILE = 3;
- static final int FLAVOR_OTHER = -1;
- private int uriFlavor;
- static final String XML_RESOURCE_PKG = "com.extensibility.xa.rsc.";
- private ResourceBundle parserErrorBundle;
- private Schema schema;
- private String flavor;
- private boolean valid;
- private boolean wellFormed;
- private Vector parseErrors;
- private String schemaPath;
- protected URI physicalURI;
- protected URI logicalURI;
- private MultiModule group;
-
- public SchemaWrapper(String var1) throws SchemaException {
- this.uriFlavor = 0;
- this.schema = null;
- this.flavor = "";
- this.valid = false;
- this.wellFormed = false;
- this.parseErrors = null;
- this.physicalURI = null;
- this.logicalURI = null;
- this.group = null;
- if (var1 == null) {
- throw new SchemaException("Cannot initialize the schema to null");
- } else {
- try {
- this.uriFlavor = 1;
- this.init();
- var1 = var1.substring(StringUtilities.skipWhite(var1, 0));
- this.physicalURI = new URI(var1);
- this.logicalURI = new URI("http://Schema");
- this.initCreate();
- } catch (Exception var3) {
- this.printConstrException(var3);
- throw new SchemaException("Cannot initialize the schema", var3);
- }
- }
- }
-
- public SchemaWrapper(URL var1) throws SchemaException {
- this.uriFlavor = 0;
- this.schema = null;
- this.flavor = "";
- this.valid = false;
- this.wellFormed = false;
- this.parseErrors = null;
- this.physicalURI = null;
- this.logicalURI = null;
- this.group = null;
- if (var1 == null) {
- throw new SchemaException("Cannot initialize the schema to null");
- } else {
- try {
- this.uriFlavor = 2;
- this.init();
- this.physicalURI = new URI(var1);
- this.logicalURI = this.physicalURI;
- this.initCreate();
- } catch (Exception var3) {
- this.printConstrException(var3);
- throw new SchemaException("Cannot initialize the schema", var3);
- }
- }
- }
-
- SchemaWrapper(URI var1, URI var2) throws SchemaException {
- this.uriFlavor = 0;
- this.schema = null;
- this.flavor = "";
- this.valid = false;
- this.wellFormed = false;
- this.parseErrors = null;
- this.physicalURI = null;
- this.logicalURI = null;
- this.group = null;
-
- try {
- this.uriFlavor = -1;
- this.init();
- this.physicalURI = var1;
- this.logicalURI = var2;
- this.initCreate();
- } catch (Exception var4) {
- this.printConstrException(var4);
- throw new SchemaException("Cannot initialize the schema", var4);
- }
- }
-
- public SchemaWrapper(File var1) throws SchemaException {
- this(new URI(var1), new URI(var1.getName()));
- this.uriFlavor = 3;
- }
-
- SchemaWrapper(File var1, MultiModule var2) throws SchemaException {
- this(var1);
- this.group = var2;
- }
-
- public SchemaWrapper() throws SchemaException {
- this.uriFlavor = 0;
- this.schema = null;
- this.flavor = "";
- this.valid = false;
- this.wellFormed = false;
- this.parseErrors = null;
- this.physicalURI = null;
- this.logicalURI = null;
- this.group = null;
-
- try {
- this.uriFlavor = 0;
- this.init();
- } catch (Exception var2) {
- this.printConstrException(var2);
- throw new SchemaException("Cannot initialize the schema", var2);
- }
- }
-
- public MultiModule getGroup() {
- return this.group;
- }
-
- public void setGroup(MultiModule var1) {
- this.group = var1;
- }
-
- public void setSchema(String var1) throws SchemaException {
- if (var1 == null) {
- throw new SchemaException("Cannot set the schema to null");
- } else {
- this.group.changedSchema(this.getFileName());
- this.uriFlavor = 1;
- String var2 = var1.substring(StringUtilities.skipWhite(var1, 0));
- this.physicalURI = URI.makeStringURI(var2);
- this.logicalURI = new URI("http://Schema");
- this.flavor = SchemaUtilities.getFlavor(this.physicalURI, true);
- if (SchemaFlavor.isFlavorSupported(this.flavor)) {
- this.schema = new Schema(this.logicalURI, this.flavor);
- } else {
- throw new SchemaException("Flavor not supported, yet");
- }
- }
- }
-
- public void setSchema(URL var1) throws SchemaException {
- if (var1 == null) {
- throw new SchemaException("Cannot set the schema to null");
- } else {
- this.group.changedSchema(this.getFileName());
- this.uriFlavor = 2;
- this.physicalURI = new URI(var1);
- this.logicalURI = this.physicalURI;
- this.flavor = SchemaUtilities.getFlavor(this.physicalURI, true);
- if (!SchemaFlavor.isFlavorSupported(this.flavor)) {
- throw new SchemaException("Flavor not supported, yet");
- } else {
- this.schema = new Schema(this.logicalURI, this.flavor);
- }
- }
- }
-
- public void setSchemaPath(String var1) {
- this.schemaPath = var1;
- }
-
- public void validate() {
- this.checkSchema();
- }
-
- public String getFlavor() {
- return this.flavor;
- }
-
- public boolean isValid() {
- return this.valid;
- }
-
- public boolean isWellFormed() {
- return this.wellFormed;
- }
-
- Enumeration getIncludedModules() {
- Vector var1 = new Vector();
- Enumeration var2 = this.schema.getDeclarations();
-
- while(var2.hasMoreElements()) {
- BaseDeclaration var3 = (BaseDeclaration)var2.nextElement();
- if (var3 instanceof ExternalSubset) {
- var1.addElement(var3);
- }
- }
-
- if (var1.isEmpty()) {
- return null;
- } else {
- return var1.elements();
- }
- }
-
- public Vector getDeclErrors() {
- if (this.schema == null) {
- return null;
- } else {
- Vector var1 = new Vector();
- Enumeration var2 = this.schema.getDeclarations();
-
- while(var2.hasMoreElements()) {
- BaseDeclaration var3 = (BaseDeclaration)var2.nextElement();
- if (var3.hasErrors() && !this.isURINotParseable(var3.getURI())) {
- Enumeration var4 = var3.getErrors().elements();
-
- while(var4.hasMoreElements()) {
- var1.addElement(var4.nextElement());
- }
- }
- }
-
- return var1;
- }
- }
-
- public int getDeclErrorsCount() {
- int var1 = 0;
- Enumeration var2 = this.schema.getDeclarations();
-
- while(var2.hasMoreElements()) {
- BaseDeclaration var3 = (BaseDeclaration)var2.nextElement();
- if (var3.hasErrors() && !this.isURINotParseable(var3.getURI())) {
- ++var1;
- }
- }
-
- return var1;
- }
-
- public Vector getDeclErrorsDesc() {
- Vector var1 = new Vector();
- Enumeration var2 = this.getDeclErrors().elements();
-
- while(var2.hasMoreElements()) {
- var1.addElement(((ParserException)var2.nextElement()).getMessage());
- }
-
- return var1.size() > 0 ? var1 : null;
- }
-
- public Vector getParseErrors() {
- if (this.parseErrors == null) {
- return null;
- } else {
- Vector var1 = new Vector(this.parseErrors.size());
- Enumeration var2 = this.parseErrors.elements();
-
- while(var2.hasMoreElements()) {
- var1.addElement((ParserException)var2.nextElement());
- }
-
- return var1;
- }
- }
-
- public int getParseErrorsCount() {
- return this.parseErrors != null ? this.parseErrors.size() : 0;
- }
-
- public Vector getParseErrorsDesc() {
- if (this.parseErrors == null) {
- return null;
- } else {
- Vector var1 = new Vector(this.parseErrors.size());
- Enumeration var2 = this.parseErrors.elements();
-
- while(var2.hasMoreElements()) {
- var1.addElement(((ParserException)var2.nextElement()).getMessage());
- }
-
- return var1;
- }
- }
-
- public String getResults() {
- if (this.schema != null) {
- if (!this.isValid()) {
- this.schema.setPrintValidatedBy(false);
- this.schema.setShowErrors(true);
- return this.writeSchema();
- } else {
- return this.getSchemaSource();
- }
- } else {
- return "";
- }
- }
-
- public String convertToDTD() {
- try {
- String var1 = this.convertTo(SchemaFlavor.DTD);
- return var1;
- } catch (SchemaException var3) {
- Object var2 = null;
- return (String)var2;
- }
- }
-
- public String convertToSOX() {
- try {
- String var1 = this.convertTo(SchemaFlavor.SOX);
- return var1;
- } catch (SchemaException var3) {
- Object var2 = null;
- return (String)var2;
- }
- }
-
- public String convertToXDR() {
- try {
- String var1 = this.convertTo(SchemaFlavor.XDR);
- return var1;
- } catch (SchemaException var3) {
- Object var2 = null;
- return (String)var2;
- }
- }
-
- public String convertToXSD() {
- try {
- String var1 = this.convertTo("xsd");
- return var1;
- } catch (SchemaException var3) {
- Object var2 = null;
- return (String)var2;
- }
- }
-
- public String convertToBIZ() {
- try {
- String var1 = this.convertTo(SchemaFlavor.BIZ);
- return var1;
- } catch (SchemaException var3) {
- Object var2 = null;
- return (String)var2;
- }
- }
-
- public String convertTo(String var1) throws SchemaException {
- if (SchemaFlavor.isFlavorSupported(var1)) {
- throw new SchemaException("Flavor not supported, yet.");
- } else if (!var1.equalsIgnoreCase(var1)) {
- if (this.schema != null && this.isValid()) {
- this.schema.setFlavor(var1);
- return this.getSchemaSource();
- } else {
- return null;
- }
- } else {
- return this.getSchemaSource();
- }
- }
-
- public String getSchemaSource() {
- if (this.schema != null) {
- this.schema.setPrintValidatedBy(true);
- this.schema.setShowErrors(false);
- return this.writeSchema();
- } else {
- return "";
- }
- }
-
- public String getFileName() {
- return this.uriFlavor > 1 && this.logicalURI != null ? this.logicalURI.getShortName() : "";
- }
-
- protected Schema getSchema() {
- return this.schema;
- }
-
- private void init() throws Exception {
- this.parserErrorBundle = ResourceBundle.getBundle("com.extensibility.xa.rsc.ParserExceptions");
- ParserException.setMessageBundle(this.parserErrorBundle);
- }
-
- private void initCreate() throws SchemaException {
- try {
- this.flavor = SchemaUtilities.getFlavor(this.physicalURI, true);
- if (SchemaFlavor.isFlavorSupported(this.flavor)) {
- this.schema = new Schema(this.logicalURI, this.flavor);
- } else {
- throw new SchemaException("Flavor not supported, yet");
- }
- } catch (Exception var2) {
- throw new SchemaException("Problems occured when thying to initialize the schema", var2);
- }
- }
-
- private void checkSchema() {
- this.valid = true;
- this.wellFormed = true;
- if (this.schema != null) {
- this.schema.removeAllDeclarations();
- this.schema.setSchemaPath(this.schemaPath);
- this.schema.setPrintValidatedBy(true);
-
- try {
- this.schema.setShowErrors(true);
- this.schema.parse(this.physicalURI, this.logicalURI, this.schema.getCount(), new Boolean(false));
- this.schema.doSecondPassValidation(this.physicalURI);
- } catch (Throwable var3) {
- this.valid = false;
- return;
- }
-
- this.filterParseErrors();
- this.checkAll();
- this.parseErrors = this.schema.getParseErrors();
- int var1 = this.getDeclErrorsCount();
- int var2 = this.schema.getCount();
- if (!this.parseErrors.isEmpty() || var1 != 0) {
- this.valid = false;
- }
- } else {
- this.valid = false;
- this.wellFormed = false;
- }
-
- }
-
- private void checkAll() {
- Enumeration var1 = this.schema.getDeclarations();
-
- while(var1.hasMoreElements()) {
- BaseDeclaration var2 = (BaseDeclaration)var1.nextElement();
- this.schema.checkForErrors(var2);
- }
-
- this.schema.checkForErrors((BaseDeclaration)null);
- }
-
- private String writeSchema() {
- StringWriter var1 = new StringWriter();
-
- try {
- this.schema.write(var1);
- String var2 = var1.toString();
- return var2;
- } catch (Exception var3) {
- return null;
- }
- }
-
- private void printConstrException(Exception var1) {
- System.out.println(var1);
- ((Throwable)var1).printStackTrace();
- }
-
- private void filterParseErrors() {
- Vector var1 = this.schema.getParseErrors();
- Vector var2 = new Vector(var1.size());
- Enumeration var3 = var1.elements();
-
- while(var3.hasMoreElements()) {
- ParserException var4 = (ParserException)var3.nextElement();
- if (this.isURINotParseable(var4.getURI())) {
- var2.addElement(var4);
- }
- }
-
- Enumeration var5 = var2.elements();
-
- while(var5.hasMoreElements()) {
- var1.removeElement(var5.nextElement());
- }
-
- }
-
- private boolean isURINotParseable(URI var1) {
- return this.checkURI(var1) || (var1.getBaseURI() != null ? this.isURINotParseable(var1.getBaseURI()) : false);
- }
-
- private boolean checkURI(URI var1) {
- Enumeration var2 = this.group.getNotParseable().elements();
-
- while(var2.hasMoreElements()) {
- SchemaWrapper var3 = (SchemaWrapper)var2.nextElement();
- if (var1.equals(var3.physicalURI)) {
- return true;
- }
- }
-
- return false;
- }
- }
-